﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace game
{
    public class FactoryPool<T>
    {
        protected Stack<T> stack;

        protected Scene scene;

        public FactoryPool(Scene scene, int initialSize)
        {
            this.scene = scene;

            stack = new Stack<T>();

            InitObjects(initialSize);
        }

        public void InitObjects(int initialSize)
        {
            for (int i = 0; i < initialSize; i++)
            {
                stack.Push(CreateObject());
            }
        }

        public T Get()
        {
            if (stack.Count > 0)
                return stack.Pop();
            else
                return CreateObject();
        }

        public void Return(T t)
        {
            stack.Push(t);
        }

        public virtual T CreateObject()
        {
            return default(T);
        }

    }
}
